package org.teamh.gui.mapeditor;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glLoadIdentity;

import org.teamh.gui.Event;
import org.teamh.gui.GUI;
import org.teamh.gui.mapeditor.MapEditorController;
import org.teamh.gui.mapeditor.MapEditorFunctions;
import org.teamh.gui.mapeditor.filechooser.NiftyFileChooser;
import org.teamh.gui.Screen;

import de.lessvoid.nifty.builder.EffectBuilder;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.PopupBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.Label;
import de.lessvoid.nifty.controls.ListBox;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.controls.label.builder.LabelBuilder;
import de.lessvoid.nifty.controls.listbox.builder.ListBoxBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.tools.Color;


public class MapEditorScreen implements Screen {

	private GUI gui;
	private MapEditorFunctions mapEditorFunctions;
	private MapEditorController controller;
	private Element downloadMapPopup;
	private Element offlinePopup;
	public static String informationtexttext="information";
	private ListBox<String> downloadmaps;
	private Element savePopup;
	private de.lessvoid.nifty.screen.Screen mapEditorScreen;
	
	private NiftyFileChooser niftyFileChooser;
	private boolean isShowing;
	
	public MapEditorScreen(GUI gui) {
		this.gui = gui;
		this.mapEditorFunctions = new MapEditorFunctions(gui);
		this.controller = new MapEditorController(gui,this);
	}
	
	@Override
	public void build() {
		 mapEditorScreen = new ScreenBuilder("mapeditor") {{
			controller(controller);
			layer(new LayerBuilder() {{
				childLayoutHorizontal();
				alignRight();
				

				//�u�eres Panel	
				panel(new PanelBuilder() {{
					onStartScreenEffect(new EffectBuilder("move") {{
						effectParameter("mode", "in");
						effectParameter("direction", "right");
						length(500); 
						startDelay(0);
						inherit(true);
						post(false);
					}});
					onEndScreenEffect(new EffectBuilder("move") {{
						effectParameter("mode", "out");
						effectParameter("direction", "right");
						length(500);
						startDelay(0);
						inherit(true);
					}});
					paddingTop("23px");
					paddingLeft("0px");
					childLayoutCenter();
					width("*");
					height("768px");
					//Gro�es �u�eres Panel um start/draw/go/nogo und die buttons,logo etc (rechts)
					panel(new PanelBuilder("playerBox") {{
						paddingTop("50px");
                        childLayoutVertical();
						alignRight();
						style("nifty-panel");
						width("280px");
						height("768px");
						paddingTop("5");
						
						//Panel um Bilder und Buttons
						interactOnClick("hideAllBordersClicked()");
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 1, 1, 0.01f));
	                        childLayoutVertical();
							//alignRight();
							//paddingRight("100px");
							paddingTop("5");
							width("245");
							height("500");
							
							//Panel um Start
							panel(new PanelBuilder() {{
								backgroundColor(new Color(0, 1, 1, 0.01f));
								paddingTop("15");
								width("245");
								height("65");
								visibleToMouse(true);
								childLayoutCenter();
								interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
							
							// Rahmen um Start
							    panel(new PanelBuilder() {{
									id("borderStart");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderStart");
									backgroundColor(new Color(0, 0, 0, 1));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(true);
							    }});//ENDE Rahmen um Start
							//angeklickter-Rahmen um Start
								panel(new PanelBuilder() {{
									id("borderStartClicked");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderStartClicked");
									backgroundColor(new Color(1f, 0.1f, 0.1f, 0.84f));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(false);
								}});//ENDE angeklickter-Rahmen um Start	
								
								
								
							//Start
							panel(new PanelBuilder() {{
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("60");
								height("60");
								visibleToMouse(true);
								childLayoutCenter();
								image(new ImageBuilder(){{
								name("startpicture");
								filename("res/img/mapeditor/start.png");
								width("60");
								height("60");
								alignCenter();
			    				valignCenter();
			    				visible(true);
			    				interactOnClick("startclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
			    				interactOnMouseOver("showInformationtextAndHovereffect(Start)");
								}});
							}});
							//Ende Start
							
							//Hovereffekt-Ebene von Start
							panel(new PanelBuilder() {{
								id("HovereffectStart");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
								name("HovereffectStart");
								backgroundColor(new Color(1, 1, 1, 0.07f));
								width("68");
								height("68");
								visibleToMouse(true);
								childLayoutAbsolute();
								interactOnClick("startclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
								interactOnMouseOver("showInformationtextAndHovereffect(Start)");
								visible(false);
							}});//ENDE Hovereffekt-Ebene von Start	
							
							}});//ENDE Panel um Start
							
							//Informationstexte Start/Draw/Go/NoGo
							panel(new PanelBuilder() {{
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("245");
								height("40");
								visibleToMouse(true);
								childLayoutCenter();
								interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
							//Informationstext zu Start
							panel(new PanelBuilder() {{
								paddingTop("2");
								text(new TextBuilder() {{
									name("informationtextStart");
									id("informationtextStart");
									font("verdana-small-regular.fnt");
									color(new Color(1,1,1,1));
									text("Startpunkt setzen");
									visible(false);
								}});
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("245");
								height("30");
								visibleToMouse(true);
								childLayoutCenter();
								visible(true);
							}});			
							
							//ENDE Informationstext zu Start
							
							//Informationstext zu Draw
							panel(new PanelBuilder() {{
								paddingTop("2");
								text(new TextBuilder() {{
									name("informationtextDraw");
									id("informationtextDraw");
									font("verdana-small-regular.fnt");
									color(new Color(1,1,1,1));
									text("Wege festlegen");
									visible(false);
								}});
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("245");
								height("30");
								visibleToMouse(true);
								childLayoutCenter();
								visible(true);
							}});			
							
							//ENDE Informationstext zu Draw
							
							//Informationstext zu Go
							panel(new PanelBuilder() {{
								paddingTop("2");
								text(new TextBuilder() {{
									name("informationtextGo");
									id("informationtextGo");
									font("verdana-small-regular.fnt");
									color(new Color(1,1,1,1));
									text("bebaubare Gebiete festlegen");
									visible(false);
								}});
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("245");
								height("30");
								visibleToMouse(true);
								childLayoutCenter();
								visible(true);
							}});
							//ENDE Informationstext zu Go
							
							//Informationstext zu NoGo
							panel(new PanelBuilder() {{
								paddingTop("2");
								text(new TextBuilder() {{
									name("informationtextNoGo");
									id("informationtextNoGo");
									font("verdana-small-regular.fnt");
									color(new Color(1,1,1,1));
									text("unbebaubare Gebiete festlegen");
									visible(false);
								}});
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("245");
								height("30");
								visibleToMouse(true);
								childLayoutCenter();
								visible(true);
							}});
							//ENDE Informationstext zu NoGo
						}});//ENDE Informationstexte Start/Draw/Go/NoGo
							
							//panel um draw/go/no go
							panel(new PanelBuilder() {{
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("245");
								height("60");
								visibleToMouse(true);
								childLayoutHorizontal();
								interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
							
								//Panel um Draw
								panel(new PanelBuilder() {{
									backgroundColor(new Color(0, 1, 1, 0.01f));
									width("81");
									height("65");
									visibleToMouse(true);
									childLayoutCenter();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
								// Rahmen um Draw
							    panel(new PanelBuilder() {{
									id("borderDraw");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderDraw");
									backgroundColor(new Color(0, 0, 0, 1));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(true);
							    }});//ENDE Rahmen um Draw
							//angeklickter-Rahmen um Draw
								panel(new PanelBuilder() {{
									id("borderDrawClicked");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderDrawClicked");
									//backgroundColor(new Color(1, 0, 0, 0.8f));
									backgroundColor(new Color(1f, 0.1f, 0.1f, 0.84f));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(false);
								}});//ENDE angeklickter-Rahmen um Draw		
							//Draw
							panel(new PanelBuilder() {{
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("81");
								height("60");
								visibleToMouse(true);
								childLayoutCenter();
								image(new ImageBuilder(){{
								filename("res/img/mapeditor/draw.png");
								width("60");
								height("60");
								alignCenter();
			    				valignCenter();
			    				visible(true);
			    				interactOnClick("drawclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
			    				interactOnMouseOver("showInformationtextAndHovereffect(Draw)");
								}});
							}});
							//Ende Draw
							
							//Hovereffekt-Ebene von Draw
							panel(new PanelBuilder() {{
								id("HovereffectDraw");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
								name("HovereffectDraw");
								backgroundColor(new Color(1, 1, 1, 0.07f));
								width("68");
								height("68");
								visibleToMouse(true);
								childLayoutAbsolute();
								interactOnClick("drawclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
								interactOnMouseOver("showInformationtextAndHovereffect(Draw)");
								visible(false);
							}});//ENDE Hovereffekt-Ebene von Draw	
							
								}});//Ende Panel um Draw
								
								
								//Panel um Go
								panel(new PanelBuilder() {{
									backgroundColor(new Color(0, 1, 1, 0.01f));
									width("81");
									height("65");
									visibleToMouse(true);
									childLayoutCenter();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
								// Rahmen um Go
							    panel(new PanelBuilder() {{
									id("borderGo");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderGo");
									backgroundColor(new Color(0, 0, 0, 1));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(true);
							    }});//ENDE Rahmen um Go
							//angeklickter-Rahmen um Go
								panel(new PanelBuilder() {{
									id("borderGoClicked");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderGoClicked");
									//backgroundColor(new Color(1, 0, 0, 0.8f));
									backgroundColor(new Color(1f, 0.1f, 0.1f, 0.84f));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(false);
								}});//ENDE angeklickter-Rahmen um Go		
							//Go
							panel(new PanelBuilder() {{
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("81");
								height("60");
								visibleToMouse(true);
								childLayoutCenter();
								image(new ImageBuilder(){{
								filename("res/img/mapeditor/go.png");
								width("60");
								height("60");
								alignCenter();
			    				valignCenter();
			    				visible(true);
			    				interactOnClick("goclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
			    				interactOnMouseOver("showInformationtextAndHovereffect(Go)");
								}});
							}});
							//Ende Go
							
							//Hovereffekt-Ebene von Go
							panel(new PanelBuilder() {{
								id("HovereffectGo");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
								name("HovereffectGo");
								backgroundColor(new Color(1, 1, 1, 0.07f));
								width("68");
								height("68");
								visibleToMouse(true);
								childLayoutAbsolute();
								interactOnClick("Goclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
								interactOnMouseOver("showInformationtextAndHovereffect(Go)");
								visible(false);
							}});//ENDE Hovereffekt-Ebene von Go	
							
							}});//Ende Panel um Go
								
								
								//Panel um NoGo
								panel(new PanelBuilder() {{
									backgroundColor(new Color(0, 1, 1, 0.01f));
									width("81");
									height("65");
									visibleToMouse(true);
									childLayoutCenter();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
								// Rahmen um NoGo
							    panel(new PanelBuilder() {{
									id("borderNoGo");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderNoGo");
									backgroundColor(new Color(0, 0, 0, 1));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(true);
							    }});//ENDE Rahmen um NoGo
							//angeklickter-Rahmen um NoGo
								panel(new PanelBuilder() {{
									id("borderNoGoClicked");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
									name("borderNoGoClicked");
									//backgroundColor(new Color(1, 0, 0, 0.8f));
									backgroundColor(new Color(1f, 0.1f, 0.1f, 0.84f));
									width("68");
									height("68");
									visibleToMouse(true);
									childLayoutAbsolute();
									interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
									visible(false);
								}});//ENDE angeklickter-Rahmen um NoGo			
							//No Go
							panel(new PanelBuilder() {{
								backgroundColor(new Color(0, 1, 1, 0.01f));
								width("81");
								height("60");
								visibleToMouse(true);
								childLayoutCenter();
								image(new ImageBuilder(){{
								filename("res/img/mapeditor/nogo.png");
								width("60");
								height("60");
								alignCenter();
			    				valignCenter();
			    				visible(true);
			    				interactOnClick("nogoclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
			    				interactOnMouseOver("showInformationtextAndHovereffect(NoGo)");
								}});
							}});
							//Ende No Go
							
							//Hovereffekt-Ebene von NoGo
							panel(new PanelBuilder() {{
								id("HovereffectNoGo");//muss man komischerweise zum benutzen der findElementByName-funktion angeben
								name("HovereffectNoGo");
								backgroundColor(new Color(1, 1, 1, 0.07f));
								width("68");
								height("68");
								visibleToMouse(true);
								childLayoutAbsolute();
								interactOnClick("nogoclick()");
			    				interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
								interactOnMouseOver("showInformationtextAndHovereffect(NoGo)");
								visible(false);
							}});//ENDE Hovereffekt-Ebene von NoGo	
							
								}});//Ende Panel um NoGo
								
								
							}});
							//ENDE panel um draw/go/no go
				
						//Panel um newMap/save/load/upload/background buttons
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 0, 1, 0.01f));
							paddingTop("20");
							width("245");
							height("315");
							visibleToMouse(true);
							childLayoutVertical();
							interactOnMouseOver("hideAllInformationtextsAndHovereffekts()");
						//newMap Button
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 0, 1, 0.01f));
							width("245");
							height("50");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("newMapButton", "NEW MAP") {{
								width("120");
								height("30");
								interactOnClick("newMap()");						
								visibleToMouse(true);
							}});
						}});
						//ENDE newMap Button
						//Load Button
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 0, 1, 0.01f));
							width("245");
							height("50");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("loadButton", "LOAD") {{
								width("120");
								height("30");
								interactOnClick("load()");
								visibleToMouse(true);
							}});
						}});
						//ENDE Load Button
						//Save Button
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 0, 1, 0.01f));
							width("245");
							height("50");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("saveButton", "SAVE") {{
								width("120");
								height("30");
								interactOnClick("save()");
								visibleToMouse(true);
							}});
						}});
						//ENDE Save Button
						
						//Upload
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 0, 1, 0.01f));
							width("245");
							height("50");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("backgroundButton", "BACKGROUND") {{
								width("120");
								height("30");
								interactOnClick("background()");
								visibleToMouse(true);
							}});
						}});
						//ENDE Upload Button
						//Background
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 0, 1, 0.01f));
							width("245");
							height("50");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("uploadButton", "UPLOAD") {{
								width("120");
								height("30");
								interactOnClick("upload()");
								visibleToMouse(true);
							}});
						}});
						//ENDE Background Button
						//Download
						panel(new PanelBuilder() {{
							backgroundColor(new Color(1, 0, 1, 0.01f));
							width("245");
							height("50");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("downloadButton", "DOWNLOAD") {{
								width("120");
								height("30");
								interactOnClick("downloadButtonclick()");
								visibleToMouse(true);
							}});
						}});
						//ENDE Background Button
						}});//ENDE Panel um newMap/save/load/upload/background buttons
						
						
						//Waypointlist
						panel(new PanelBuilder() {{
							paddingTop("8");
							paddingLeft("22");
							childLayoutVertical();
							width("240");
							height("190");
							text(new TextBuilder() {{
								font("verdana-small-regular.fnt");
								color(new Color(1,1,1,1));
								text("     List of Waypoints");
							}});
							control(new ListBoxBuilder("wayPointList"){{
								style("nifty-listbox");
								hideHorizontalScrollbar();
								optionalVerticalScrollbar();
								selectionModeSingle();
								width("200");
								displayItems((768/64)-5);
							}});
							
						}});
						//Ende Waypointlist
						
//						//Logo
//						panel(new PanelBuilder() {{
//							paddingTop("0");
//							backgroundColor(new Color(0, 1, 1, 0.01f));
//							width("245");
//							height("155");
//							visibleToMouse(true);
//							childLayoutCenter();
//							interactOnClick("hideInformationtext()");
//							image(new ImageBuilder(){{
//							filename("res/img/logo/wasp_edited.png");
//							width("92");
//							height("120");
//							alignCenter();
//		    				valignCenter();
//		    				visible(true);
//		    				//Fadeeffekt
//		    				/*
//		    				onActiveEffect(new EffectBuilder("colorPulsate") {{
//		    		        effectParameter("period", "7000");
//		    		        effectParameter("startColor", "#44444444");
//		    		        effectParameter("endColor", "#dddddddd");
//		    				}});
//		    				//FadeEffekt
//									onActiveEffect(new EffectBuilder("colorPulsate") {{
//					    		        effectParameter("period", "2500");
//					    		        effectParameter("startColor", "#ffffffff");
//					    		        effectParameter("endColor", "#88888888");
//					    				}});
//							//Ende FadeEffekt
//		    				*/
//		                    //Ende Fadeeffekt
//							}});
//						}});						
//						//ENDE Logo
//						//Wasp Enterprises Text
//						panel(new PanelBuilder() {{
//							paddingTop("2");
//							text(new TextBuilder() {{
//								font("verdana-small-regular.fnt");
//								color(new Color(1,1,1,1));
//								text("Wasp Enterprises");
//							}});
//							backgroundColor(new Color(0, 1, 1, 0.01f));
//							width("245");
//							height("30");
//							visibleToMouse(true);
//							childLayoutCenter();
//						}});						
//						//ENDE Wasp Enterprises Text
//						
//						//CreepSmash Text
//						panel(new PanelBuilder() {{
//							paddingTop("1");
//							text(new TextBuilder() {{
//								font("res/font/aurulent-sans-36.fnt");
//								color(new Color(1,1,1,1));
//								text("CreepSmash");
//							}});
//							backgroundColor(new Color(0, 1, 1, 0.01f));
//							width("245");
//							height("30");
//							visibleToMouse(true);
//							childLayoutCenter();
//						}});						
//						//ENDE CreepSmash Text
						
						//Panel um Help und Exitbutton
						panel(new PanelBuilder() {{
							backgroundColor(new Color(0, 1, 1, 0.01f));
							paddingTop("0");
							width("245");
							height("60");
							visibleToMouse(true);
							childLayoutHorizontal();
						//Help Button
						panel(new PanelBuilder() {{
							backgroundColor(new Color(0, 1, 1, 0.01f));
							paddingTop("0");
							width("122");
							height("60");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("helpButton", "WALK") {{
								width("80");
								height("20");
								interactOnClick("help()");
								visibleToMouse(true);
							}});
						}});
						//ENDE Help Button
						//Exit Button
						panel(new PanelBuilder() {{
							backgroundColor(new Color(0, 1, 1, 0.01f));
							paddingTop("0");
							width("122");
							height("60");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("exitButton", "EXIT") {{
								width("80");
								height("20");
								interactOnClick("exit()");
								visibleToMouse(true);
							}});
						}});
						//ENDE Exit Button
						
						}});//ENDE Panel um Help und Exitbutton
						
						
						}});//ENDE Panel um Bilder und Buttons
						
						
					}});				
					panel(new PanelBuilder() {{
						alignLeft();
						childLayoutAbsolute();	
						paddingTop("50px");						
					}});
					
				}});
				//ENDE Gro�es �u�eres Panel um start/draw/go/nogo und die buttons,logo etc (rechts)
			}});
			//ENDE �u�eres Panel
		}}.build(gui.getNifty());
		gui.getNifty().addScreen("mapeditor", mapEditorScreen);
		//DownloadMapPopup
	}
	
	@SuppressWarnings("unchecked")
	public void initPopup() {
		
		new PopupBuilder("downloadMapPopup") {
			{
				childLayoutCenter();
				backgroundColor("#000a");
				width("968");
			    height("*");
			    
    
				//Login und Mapdownloadtext/List/Button
				 panel(new PanelBuilder() {{				
					childLayoutCenter();
					width("350");
				    height("320");
				    style("nifty-panel");
				    visible(true);
				    //LOGIN Downloadmap
					panel(new PanelBuilder("mapDownloadLoginPanel") {{	
						childLayoutVertical();
						visible(true);
						paddingTop("50");
						paddingLeft("50");
						width("350");
					    height("320");
					    panel(new PanelBuilder() {{	
							childLayoutCenter();
							paddingLeft("40");
							text(new TextBuilder() {{
								width("160");
							    height("60");
								font("verdana-small-regular.fnt");
								color(new Color(1,1,1,1));
								text("Please login to download\n" +
										" a map from server.");
							}});
							
					    }});
						
				    panel(new PanelBuilder() {{ // Oben
						paddingTop("10");
						height("40");
						childLayoutHorizontal();
						
						//Effekt Name-Texfeld
						panel(new PanelBuilder() {{ // oben links
							name("LoginNameTextfieldEffect");
							id("LoginNameTextfieldEffect");
							childLayoutAbsoluteInside();
							paddingLeft("50");
							width("0");
							text(new TextBuilder() {{
								font("verdana-small-regular.fnt");
								color(new Color(1,1,1,1));
								text("Name");
								onActiveEffect(new EffectBuilder("colorPulsate") {{
				    		        effectParameter("period", "1600");
				    		        effectParameter("startColor", "#00000000");
				    		        effectParameter("endColor", "#ffffffff");
				    				}});	
							}});
							visible(true);
						}});
						//ENDE Effekt Name-Texfeld
						
						panel(new PanelBuilder() {{ // oben links
							name("LoginNameTextfield");
							id("LoginNameTextfield");
							childLayoutAbsoluteInside();
							width("130");
							text(new TextBuilder() {{
								font("verdana-small-regular.fnt");
								color(new Color(1,1,1,1));
								text("Name");
							}});
							visible(true);
						}});
						
						panel(new PanelBuilder() {{ // oben rechts
							childLayoutAbsoluteInside();
							visibleToMouse(true);
							control(new TextFieldBuilder("eingabeName") {{
								width("100");
								font("verdana-small-regular.fnt");
								//Effekt von Nametextfeld aufrufen 
								 interactOnClick("changeVisibility(LoginNameTextfieldEffect,LoginPasswordTextfieldEffect)");//das erste Argument wird sichtbar, das zweite unsichtbar
	                            //ENDE Effekt von Nametextfeld aufrufen 
							}});
						}});
					}});
					panel(new PanelBuilder() {{ // mitte
						childLayoutHorizontal();
						height("40");
						//Effekt Passwort-Texfeld
						panel(new PanelBuilder() {{//mitte links
							name("LoginPasswordTextfieldEffect");
							id("LoginPasswordTextfieldEffect");
							paddingLeft("70");
							childLayoutAbsoluteInside();
							width("0");
							text(new TextBuilder() {{
								font("verdana-small-regular.fnt");
								text("Passwort");
								onActiveEffect(new EffectBuilder("colorPulsate") {{
				    		        effectParameter("period", "1600");
				    		        effectParameter("startColor", "#00000000");
				    		        effectParameter("endColor", "#ffffffff");
				    				}});
							visible(false);	
							}});
						}});
						//ENDE Effekt Passwort-Texfeld
						panel(new PanelBuilder() {{//mitte links
							childLayoutAbsoluteInside();
							width("130");
							text(new TextBuilder() {{
								font("verdana-small-regular.fnt");
								text("Passwort");
								
							}});
						}});
						panel(new PanelBuilder() {{// mitte rechts
							childLayoutAbsoluteInside();
							visibleToMouse(true);
							control(new TextFieldBuilder("eingabePasswort") {{
								passwordChar('*');
								width("100");
								font("verdana-small-regular.fnt");
								//Effekt von Passworttextfeld aufrufen 
								 interactOnClick("changeVisibility(LoginPasswordTextfieldEffect,LoginNameTextfieldEffect)");//das erste Argument wird sichtbar, das zweite unsichtbar
	                            //Ende Effekt von Passworttextfeld  aufrufen 
							}});
						}});
					}});
					panel(new PanelBuilder() {{		//falscher login
						height("20");
						width("270");
						childLayoutCenter();
						text(new TextBuilder("wrongLogin") {{
							font("verdana-small-regular.fnt");
							color(new Color(1,0,0,1));
							text("wrong login!");
							visible(false);
						}});
					}});

						panel(new PanelBuilder(){{
							height("30");
							width("130");
							paddingLeft("230");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("loginButton", "LOGIN") {{
								alignCenter();
								interactOnClick("loginbuttonclick()");
								visibleToMouse(true);
							}});
						}});


				 }});//ENDE LOGIN Downloadmap	 

				    
				    
				  //Mapdownloadtext/List/Button#
					panel(new PanelBuilder("mapDownloadPanel") {{
						childLayoutVertical();
						visible(false);
						paddingTop("14");
						paddingLeft("124");
						width("350");
					    height("320");
				 //Mapdownloadtext
					panel(new PanelBuilder() {{
						paddingTop("15");
						childLayoutCenter();
						width("100");
						height("30");
						text(new TextBuilder() {{
							name("chooseDownloadMaptext");
							id("chooseDownloadMaptext");
							font("verdana-small-regular.fnt");
							color(new Color(1,1,1,1));
							text("CHOOSE MAP");
						}});
						
					}});
					//Ende Mapdownloadtext
				   
				   
				 //Mapdownloadlist
					panel(new PanelBuilder() {{
						childLayoutCenter();
						width("100");
						height("190");
						control(new ListBoxBuilder("mapdownloadlist"){{
							style("nifty-listbox");
							hideHorizontalScrollbar();
							optionalVerticalScrollbar();
							selectionModeSingle();
							width("250");
							displayItems((768/64)-5);
						}});
						
					}});
					//Ende Mapdownloadlist
					
					 //MapdownloadButton
					panel(new PanelBuilder() {{
						childLayoutCenter();
						width("100");
						height("30");
						control(new ButtonBuilder("mapDownloadbutton", "DOWNLOAD") {{
							width("120");
							height("30");
							interactOnClick("mapdownloadbuttonclick()");						
							visibleToMouse(true);
						}});						
					}});//Ende MapdownloadButton
				}}); //Mapdownloadtext/List/Button
					//Backbutton

						panel(new PanelBuilder(){{
							height("30");
							width("130");
							paddingTop("270");
							alignLeft();
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("BackToMapEditorButton", "Quit") {{
								valignCenter();
								interactOnClick("backToMapEditorButtonclick()");
								visibleToMouse(true);
							}});
					    }});//Backbutton	
				 }}); //Login und Mapdownloadtext/List/Button

			}}.registerPopup(gui.getNifty());
			
			downloadMapPopup=gui.getNifty().createPopup("downloadMapPopup");
			
			downloadmaps = downloadMapPopup.findNiftyControl("mapdownloadlist", ListBox.class);
			//ENDE DownloadMapPopup
			
			//start is offline popup
			new PopupBuilder("isOfflinePopup") {{
				childLayoutCenter();
				backgroundColor("#000a");
				width("968");
			    height("*");
			    
			    panel(new PanelBuilder() {{
			    	width("300px");
			    	height("200px");
			    	style("nifty-panel");
			    	childLayoutVertical();
			    	
			    	panel(new PanelBuilder() {{
			    		width("*");
			    		height("*");
			    		childLayoutCenter();
			    		
			    		control(new LabelBuilder() {{
							text("Server is currently offline");
							color("#ffffff");
							width("400px");
						}});
			    	}});
			    	
			    	panel(new PanelBuilder() {{
			    		width("*");
			    		height("*");
			    		childLayoutCenter();
			    		
			    		control(new ButtonBuilder("serverOfflineOK", "OK") {{
			    			visibleToMouse();
			    			interactOnClick("isOfflineOKClick()");
			    		}});
			    	}});
			    }});
			}}.registerPopup(gui.getNifty());
			//end is offline popup
			
			offlinePopup = gui.getNifty().createPopup("isOfflinePopup");
			
			//create the nifty file chooser with its popup
			this.niftyFileChooser = new NiftyFileChooser(gui.getNifty(), mapEditorScreen);
	}
	
	public MapEditorFunctions getMapEditorFunctions(){
		return mapEditorFunctions;
	}
	
	@Override
	public void render(float timeElapsed) {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		mapEditorFunctions.render();
		
		if(niftyFileChooser != null) {
			niftyFileChooser.render();
		}
	}

	@Override
	public void init() {}

	@Override
	public void keyPressed(int key) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(int key) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(int posX, int posY, int button) {
		controller.setButton(button);
		controller.chooseClickFunction(posX,posY);
		/*
		if(button == 0){
			controller.chooseLeftClickFunction(posX, posY);
		}
		if(button == 1){
			controller.chooseRightClickFunction(posX, posY);
		}
		*/
	}

	@Override
	public void mouseReleased(int posX, int posY, int button) {
		controller.mouseReleased(posX, posY);
	}

	@Override
	public void mouseMoved(int posX, int posY, int button) {
		
	}

	@Override
	public void mouseDragged(int posX, int posY, int button) {
			controller.chooseClickFunction(posX, posY);

	}

	@Override
	public void mouseWheelMoved(int movement) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dealloc() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventOccured(Event event) {
		if(event.getKey().equals("login")) {
			boolean status = Boolean.parseBoolean(event.getValue());
			
			if(status) {
				gui.getStart().getConnection().sendMessage("LIST MAPS");
				controller.changeVisibility("mapDownloadPanel","mapDownloadLoginPanel");
				controller.setExitClicked(false);	
				controller.setLoggedIn(true);
				if (controller.isUploadClicked()) {
					controller.upload();
					gui.getNifty().closePopup(downloadMapPopup.getId());	
				}
			}
			mapEditorScreen.findElementByName("wrongLogin").setVisible(!status);
		} else if(event.getKey().equals("upload")) {
			gui.getNifty().getCurrentScreen().findNiftyControl("errorPopupText", Label.class)
			.setText("Map has been saved but wasn't uploaded!");
		}
	}
	
	public Element getDownloadMapPopup() {
		return downloadMapPopup;
	}

	public ListBox<String> getMaps() {
		return downloadmaps;
	}
	
	public MapEditorController getController() {
		return controller;
	}
	
	public de.lessvoid.nifty.screen.Screen getMapEditorScreen() {
		return mapEditorScreen;
	}
	
	public NiftyFileChooser getNiftyFileChooser() {
		return niftyFileChooser;
	}
	
	public boolean canPoll() {
		return true;
	}
	
	public void showSavePopup() {
		setShowing(true);
		gui.getNifty().showPopup(gui.getNifty().getScreen("ingameScreen"), savePopup.getId(), null);
	}
	
	public void closeSavePopup() {
		setShowing(false);
		gui.getNifty().closePopup(savePopup.getId());
	}
	
	/**
	 * This method opens a popup, which shows the user, that the server is currently offline. 
	 */
	public void showIsOfflinePopup() {
		gui.getNifty().showPopup(mapEditorScreen, offlinePopup.getId(), null);
	}
	
	/**
	 * This method closes the popup, which shows, the user, that the server is currently offline. 
	 */
	public void closeIsOfflinePopup() {
		gui.getNifty().closePopup(offlinePopup.getId());
	}

	public void setShowing(boolean isShowing) {
		this.isShowing = isShowing;
	}

	public boolean isShowing() {
		return isShowing;
	}
	
	@Override
	public String getNiftyScreenID() {
		return "mapeditor";
	}
}
